{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solving BL in python\n",
    "This is another try to solve BL equation in FiPy. I gave up some time ago without actually giving it a proper trial. Now I'm going to try it out here with the new version of FiPy that I have installed in Conda."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "phi = 0.4 # porosity\n",
    "nw = 2.0\n",
    "no = 2.0\n",
    "krw0 = 0.4\n",
    "kro0 = 1.0\n",
    "swc = 0.15\n",
    "sor = 0.2\n",
    "mu_w = 0.001 # Pa.s\n",
    "mu_o = 0.002 # Pa.s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def kro(sw, kro0, sor, swc, no):\n",
    "    res = ((swc<=sw) & (sw<=1-sor))*kro0*((1-sw-sor)/(1-sor-swc))**no \\\n",
    "    +((0.0<sw) & (sw<swc))*(1+(kro0-1)/swc*sw) \\\n",
    "    +(sw>1-sor)*0.0 \\\n",
    "    +(sw<=0.0)*1.0\n",
    "    return res\n",
    "\n",
    "def krw(sw, krw0, sor, swc, nw):\n",
    "    res = ((swc<=sw) & (sw<=1-sor))*krw0*((sw-swc)/(1-sor-swc))**nw \\\n",
    "    +((1-sor<sw) & (sw<1.0))*(-(1-krw0)/sor*(1.0-sw)+1.0) \\\n",
    "    +(sw<=swc)*0.0 \\\n",
    "    +(sw>=1.0)*1.0\n",
    "    return res\n",
    "\n",
    "def dkrodsw(sw, kro0, sor, swc, no):\n",
    "    res = ((swc<=sw) & (sw<=1-sor))*(-no*kro0/(1-sor-swc)*((1-sw-sor)/(1-sor-swc))**(no-1)) \\\n",
    "    +((0.0<sw) & (sw<swc))*(kro0-1)/swc \\\n",
    "    +((sw>1-sor) | (sw<=0.0))*0.0\n",
    "    return res\n",
    "\n",
    "def dkrwdsw(sw, krw0, sor, swc, nw):\n",
    "    res = ((swc<=sw) & (sw<=1-sor))*nw*krw0/(1-sor-swc)*((sw-swc)/(1-sor-swc))**(nw-1) \\\n",
    "    +((1-sor<sw) & (sw<1.0))*(1-krw0)/sor \\\n",
    "    +((sw<swc) | (sw>=1.0))*0.0\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fipy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nx = 50\n",
    "L = 0.12 # domain length\n",
    "u_inj = 1.0e-5 # m/s injection velocity\n",
    "mesh = fipy.Grid1D(nx = nx, Lx = L)\n",
    "sw_inj = 1.0 # left boundary condition\n",
    "sw_init = swc\n",
    "sw0 = swc\n",
    "\n",
    "p_out = 1.0e5 # right boundary pressure\n",
    "p_in  = 2.0e5 # left boundary pressure (making it simple)\n",
    "p0 = p_out\n",
    "\n",
    "sw = fipy.CellVariable(name=\"saturation\", mesh=mesh, value=sw0, hasOld=True)\n",
    "p  = fipy.CellVariable(name=\"pressure\", mesh=mesh, value=p0, hasOld=True)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "? fipy.Grid1D"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
